Ext.onReady(function() {
	var canNew = true;
	var newDeviceApply = {
		text : '新建申请单',
		xtype : 'button',
		listeners : {
			click : function() {
				location.href = "/wuziMgr/add_wz_apply";
			}
		}
	};
	var deleteDeviceApply = {
		text : '删除',
		xtype : 'button',
		listeners : {
			click : function() {
				var grid = Ext.getCmp("wz_apply_grid");
				var selectionModel = grid.getSelectionModel();
				var models = selectionModel.getSelection();
				var ids = [];
				for (var i = 0; i < models.length; i++) {
					ids[i] = models[i].get("Id");
				}
				Ext.Ajax.request({
					url : '/device_apply/delete',
					type : 'POST',
					params : {
						ids : ids
					},
					callback : function(options, success, response) {
						if (success) {
							var result = Ext.decode(response.responseText);
							if (result.success) {
								Ext.getCmp("wz_apply_grid").getStore().reload();
							}
						}
					}
				});
			}
		}
	};
	var refreshDeviceApply = {
		text : '刷新',
		xtype : 'button',
		listeners : {
			click : function() {
				Ext.getCmp("wz_apply_grid").getStore().reload();
			}
		}
	};
	var readyDeviceApply = {
		text : '待申请清单',
		xtype : 'button',
		listeners : {
			click : function() {
				location.href = "/forapply/detail";
			}
		}
	};
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
				dock : 'top',
				items : []
			});
	Ext.Ajax.request({
				url : 'privilege/has_privilege',
				params : {
					action : '/device_apply/add'
				},
				callback : function(options, success, response) {
					if (success) {
						try {
							var ar = Ext.decode(response.responseText);
							canNew = ar.data;
						} catch (e) {
							canNew = false;
						}
					} else {
						canNew = false;
					}
					if (canNew) {
						toolbar.add(newDeviceApply);
						toolbar.add(deleteDeviceApply);
					}
					toolbar.add(refreshDeviceApply);
					toolbar.add(readyDeviceApply);
				}
			});

	var store = Ext.create('Ext.data.Store', {
				fields : ['Id', "OrderNo", {
							name : "Department",
							type : "object"
						}, {
							name : "Group",
							type : "object"
						}, {
							name : "Time"/*,
							type : "date"*/
						}],
				autoLoad : true,
				pageSize: 25,
				proxy : {
					type : 'ajax',
					url : '/device_apply/all',
					reader : {
						type : 'json',
						root : 'data.rows',
						totalProperty: 'data.total'
					}
				}
			});
	
	var grid = Ext.create('Ext.grid.Panel', {
				id : "wz_apply_grid",
				selType : 'checkboxmodel',
				store : store,
				margin : '5 5',
				columns : [{
							text : '序号',
							xtype : 'rownumberer',
							width : 40,
							sortable : false
						}, {
							text : '订单号',
							dataIndex : 'OrderNo',
							flex : 1,
							renderer : function(v) {
								return v;
							}
						}, {
							text : '申请部门',
							dataIndex : 'Department',
							flex : 1,
							renderer : function(v) {
								return v.Name;
							}
						}, {
							text : '申请班组',
							flex : 1,
							dataIndex : 'Group',
							renderer : function(v) {
								return v.Name;
							}
						}, {
							text : '申请时间',
							dataIndex : 'Time',
							flex : 1,
							renderer : function(v) {
								return v;
							}
						}, {
							text : '详细',
							width : 60,
							renderer : function(v, metadata, record) {
								return '<a href="device_apply/detail/'
										+ record.get("Id") + '">详细</a>';
							}
						}],
						bbar : Ext.create('Ext.PagingToolbar', {
							store : store,
							border: false,
							displayInfo : true,
							displayMsg : '显示{0} - {1} / 总数 {2}',
							emptyMsg : "没数据"
						})
			});

	var panel = Ext.create("Ext.panel.Panel", {
				title : '',
				border : false,
				bodyBorder : false,
				layout : 'fit',
				dockedItems : [toolbar],
				items : [grid]
			});
	Ext.create('Ext.Viewport', {
				layout : 'fit',
				style : 'background:none;background-color:white;',
				items : [panel]
			});
});